#include "../GameplayStates/GameplayState.h"
#include "../GameplayStates/MainMenuState.h"
#include "Player.h"

#include "../Abilities/Spin.h"
#include "../Abilities/Shield.h"
#include "../Abilities/Dash.h"

#include "Game.h"
#include "../GameplayStates/OptionsState.h"
#include "DestroyEntityMsg.h"
#include "../GameplayStates/GameOverState.h"
#include "../GameplayStates/HighScoresState.h"
#include "../SGD Wrappers/SGD_String.h"
using std::ostringstream;
//#include "FlyingEnemy.h"

CGameplayState* CGameplayState::GetInstance(void)
{
	static CGameplayState s_Instance;
	return &s_Instance;
}

CGameplayState::CGameplayState(void)
{
	this->m_nHealthCostImgID = -1;
	this->m_nLifeBarFrameImgID = -1;
	this->m_nLifeBarImgID	= -1;
	paused					= false;
	curMove					= 0;
	this->m_nBackGroundMusicID	= -1;
	m_pFlyingEnemy = nullptr;
	m_nBackGroundImgID = -1;
	m_bSaved = false;

	m_nPlayerAttackSFXID	= -1;
	m_nPlayerJumpSFXID		= -1;
	m_nPlayerDeathSFXID		= -1;
	m_nEnemyAttackSFXID		= -1;
	m_nEnemyDeathSFXID		= -1;


	spawnscount=0;
}

CGameplayState::~CGameplayState(void) { }

void CGameplayState::Enter(void)
{
	this->m_pD3D = CSGD_Direct3D::GetInstance();
	this->m_pDI = CSGD_DirectInput::GetInstance();
	this->m_pES = CEventSystem::GetInstance();
	this->m_pMS = CMessageSystem::GetInstance();
	this->m_pMS->InitMessageSystem(&MessageProc);
	this->m_pOF = CObjectFact::GetInstance();
	this->m_pOM = CObjectManager::GetInstance();

	this->m_pTM = CSGD_TextureManager::GetInstance();
	this->m_pXA = CSGD_XAudio2::GetInstance();
	this->m_pAM = CAnimationManager::GetInstance();
	this->m_pSS	= CSpawnSystem::GetInstance();
	this->m_pST	= CScoreTimer::GetInstance();
	m_pST->Reset();

	m_pOF->RegisterClassType<CEntity>(L"CEntity");
	m_pOF->RegisterClassType<CPlayer>(L"CPlayer");
	m_pOF->RegisterClassType<CSpin>(L"CSpin");
	m_pOF->RegisterClassType<CShield>(L"CShield");
	m_pOF->RegisterClassType<CDash>(L"CDash");
	//	m_pOF->RegisterClassType<CFlyingEnemy>(L"CFlyingEnemy");

	CEventSystem::GetInstance()->RegisterClient("Jumped",this);
	CEventSystem::GetInstance()->RegisterClient("Death",this);
	CEventSystem::GetInstance()->RegisterClient("Attacked",this);
	CEventSystem::GetInstance()->RegisterClient("EDeath",this);
	CEventSystem::GetInstance()->RegisterClient("EAttacked",this);
	CEventSystem::GetInstance()->RegisterClient("PlayerLand",this);
	CEventSystem::GetInstance()->RegisterClient("BDeath",this);
	CEventSystem::GetInstance()->RegisterClient("BAttacked",this);
	CEventSystem::GetInstance()->RegisterClient("BSummon",this);
	m_pAM->LoadFile("../resources/XML/PLAYERANIMATIONS.xml");
	m_pAM->LoadFile("../resources/XML/Enemy_Basic.xml");
	m_pAM->LoadFile("../resources/XML/Enemy_Boss1.xml");
	//m_pAM->LoadFile("../resources/XML/Enemy_Boss2.xml");
	//m_pAM->LoadFile("../resources/XML/Enemy_Jumper.xml");
	//m_pAM->LoadFile("../resources/XML/Enemy_Shooter.xml");
	//m_pAM->LoadFile("../resources/XML/Enemy_Flying.xml");

	m_pAM->SetMap();

	this->m_pPlayer = m_pOF->CreateObject(L"CPlayer");
	this->m_pSpin = m_pOF->CreateObject(L"CSpin");
	this->m_pShield = m_pOF->CreateObject(L"CShield");
	this->m_pDash = m_pOF->CreateObject(L"CDash");

	CPlayer* pPlayer2 = dynamic_cast<CPlayer*>(m_pPlayer);
	pPlayer2->SetImgId(m_pAM->m_mapAnimations["PlayerIdle"]->GetImageID());		//add this ie. " nemo"
	pPlayer2->Init();	//resets once you enter the level

	this->m_nLifeBarImgID = m_pTM->LoadTexture(L"../resources/graphics/HealthBarFill2.png", D3DCOLOR_XRGB(255, 255, 255));
	this->m_nLifeBarFrameImgID = m_pTM->LoadTexture(L"../resources/graphics/HealthBarFrame2.png", D3DCOLOR_XRGB(255, 255, 255));
	this->m_nHealthCostImgID = m_pTM->LoadTexture(L"../resources/graphics/LifelossBar2.png", D3DCOLOR_XRGB(255, 255, 255));
	this->m_nSpinButtonImgID = m_pTM->LoadTexture(L"../resources/graphics/SpinIcon.png", D3DCOLOR_XRGB(255, 255, 255));

	//SOUND---------------------------------------------------------------------------------------------------
	this->m_nBackGroundMusicID = m_pXA->MusicLoadSong(_T("../resources/sounds/CJM_BackGroundMusic.xwm"));
	if (!m_pXA->MusicIsSongPlaying(m_nBackGroundMusicID))
	{
		m_pXA->MusicStopSong( m_nBackGroundMusicID );
		m_pXA->MusicPlaySong(m_nBackGroundMusicID, true);
	}


	this->m_nPlayerAttackSFXID	=	m_pXA->SFXLoadSound(_T("../resources/sounds/PlayerAttack.wav"));
	this->m_nPlayerJumpSFXID	=	m_pXA->SFXLoadSound(_T("../resources/sounds/PlayerJump.wav"));
	this->m_nPlayerDeathSFXID	=	m_pXA->SFXLoadSound(_T("../resources/sounds/PlayerDeath.wav"));
	this->m_nEnemyAttackSFXID	=	m_pXA->SFXLoadSound(_T("../resources/sounds/EnemyAttack.wav"));
	this->m_nEnemyDeathSFXID	=	m_pXA->SFXLoadSound(_T("../resources/sounds/EnemyDeath.wav"));
	this->m_nPlayerLandSFXID =	m_pXA->SFXLoadSound(_T("../resources/sounds/PlayerLand.wav"));
	this->m_nBossSummonSFXID =	m_pXA->SFXLoadSound(_T("../resources/sounds/BossSummon.wav"));
	this->m_nBossAttackSFXID =	m_pXA->SFXLoadSound(_T("../resources/sounds/BossAttack.wav"));
	this->m_nBossDeadSFXID	 =	m_pXA->SFXLoadSound(_T("../resources/sounds/BossDeath.wav"));




	COptionsState::GetInstance()->LoadVolume("../resources/sounds/volume.txt");
	//-------------------------------------------------------------------------------

	CPlayer* pPlayer = dynamic_cast<CPlayer*>(m_pPlayer);
	pPlayer->SetImgId(m_pAM->m_mapAnimations["PlayerIdle"]->GetImageID());
	m_pOM->AddObject(pPlayer);
	pPlayer->Init();
	this->paused = false;
	m_pFont.Load("../resources/XML/DeadlyShadowsFont.xml");



	//m_nBackGroundImgID = m_pTM->LoadTexture(L"../resources/graphics/Tiles01.png", D3DCOLOR_XRGB(255, 255, 255));



}

void CGameplayState::Exit(void)
{
	CEventSystem::GetInstance()->UnregisterClientAll(this);
	m_pAM->Exit();

	//SOUND
	if( m_nBackGroundMusicID != -1 )
	{
		m_pXA->MusicStopSong( m_nBackGroundMusicID );
		m_pXA->MusicUnloadSong( m_nBackGroundMusicID );
		m_nBackGroundMusicID = -1;
	}
	if(this->m_pPlayer)
	{
		this->m_pPlayer->Release();
		this->m_pPlayer = nullptr;
	}

	if(this->m_pSpin)
	{
		this->m_pSpin->Release();
		this->m_pSpin = nullptr;
	}

	if(this->m_pES)
	{
		this->m_pES->ShutdownEventSystem();
		this->m_pES = nullptr;
	}

	if(this->m_pMS)
	{
		this->m_pMS->ShutdownMessageSystem();
		this->m_pMS = nullptr;
	}

	if(this->m_pOF)
	{
		this->m_pOF->ShutDownObjectFactoy();
		this->m_pOF = nullptr;
	}

	if(this->m_pOM)
	{
		this->m_pOM->RemoveEVERYTHING();
		this->m_pOM->DeleteInstance();
		this->m_pOM = nullptr;
	}
	if (this->m_nBackGroundImgID != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nBackGroundImgID);
		this->m_nBackGroundImgID = -1;
	}
	if(this->m_nHealthCostImgID != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nHealthCostImgID);
		this->m_nHealthCostImgID = -1;
	}
	if(this->m_nLifeBarFrameImgID != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nLifeBarFrameImgID);
		this->m_nLifeBarFrameImgID = -1;
	}
	if(this->m_nLifeBarImgID != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nLifeBarImgID);
		this->m_nLifeBarImgID = -1;
	}

	if(this->m_nSpinButtonImgID != -1)
	{
		this->m_pTM->UnloadTexture(this->m_nSpinButtonImgID);
		this->m_nSpinButtonImgID = -1;
	}

	if(this->m_nPlayerAttackSFXID	!= -1)
	{
		m_pXA->SFXStopSound(m_nPlayerAttackSFXID);
		m_pXA->SFXUnloadSound(m_nPlayerAttackSFXID);
		this->m_nPlayerAttackSFXID	= -1;
	}
	if(this->m_nPlayerJumpSFXID		!= -1)
	{
		m_pXA->SFXStopSound(m_nPlayerJumpSFXID);
		m_pXA->SFXUnloadSound(m_nPlayerJumpSFXID);
		this->m_nPlayerJumpSFXID	= -1;
	}
	if(this->m_nPlayerDeathSFXID	!= -1)
	{
		m_pXA->SFXStopSound(m_nPlayerDeathSFXID);
		m_pXA->SFXUnloadSound(m_nPlayerDeathSFXID);
		this->m_nPlayerDeathSFXID	= -1;
	}
	if(this->m_nEnemyAttackSFXID	!= -1)
	{
		m_pXA->SFXStopSound(m_nEnemyAttackSFXID);
		m_pXA->SFXUnloadSound(m_nEnemyAttackSFXID);
		this->m_nEnemyAttackSFXID	= -1;
	}
	if(this->m_nEnemyDeathSFXID		!= -1)
	{
		m_pXA->SFXStopSound(m_nEnemyDeathSFXID);
		m_pXA->SFXUnloadSound(m_nEnemyDeathSFXID);
		this->m_nEnemyDeathSFXID	= -1;
	}
	if(this->m_nEnemyDeathSFXID		!= -1)
	{
		m_pXA->SFXStopSound(m_nBossDeadSFXID);
		m_pXA->SFXUnloadSound(m_nBossDeadSFXID);
		this->m_nBossDeadSFXID	= -1;
	}
	if(this->m_nPlayerLandSFXID		!= -1)
	{
		m_pXA->SFXStopSound(m_nPlayerLandSFXID);
		m_pXA->SFXUnloadSound(m_nPlayerLandSFXID);
		this->m_nPlayerLandSFXID	= -1;
	}
	if(this->m_nBossSummonSFXID		!= -1)
	{
		m_pXA->SFXStopSound(m_nBossSummonSFXID);
		m_pXA->SFXUnloadSound(m_nBossSummonSFXID);
		this->m_nBossSummonSFXID	= -1;
	}
	if(this->m_nBossAttackSFXID		!= -1)
	{
		m_pXA->SFXStopSound(m_nBossAttackSFXID);
		m_pXA->SFXUnloadSound(m_nBossAttackSFXID);
		this->m_nBossAttackSFXID	= -1;
	}







}

bool CGameplayState::Input(void)
{

	if(m_pDI->KeyPressed(DIK_ESCAPE))
	{
		paused = !paused;
	}
	if (paused)
	{
		if (m_pDI->KeyPressed(DIK_UP ))
		{
			if (curMove == 0)
			{
				curMove = 3; 
			}
			curMove -=1;
			m_bSaved = false;
		}

		if (m_pDI->KeyPressed(DIK_DOWN))
		{
			if (curMove == 2)
			{
				curMove = -1;
			}
			curMove += 1;
		}
		//=================================================================================================

		if (curMove == 0)
		{
			if (m_pDI->KeyPressed(DIK_RETURN))
			{
				paused = !paused;
			}
		}

		if (curMove == 1)
		{
			if (m_pDI->KeyPressed(DIK_RETURN))
			{
				CGame::GetInstance()->AddState(CMainMenuState::GetInstance());
				CGame::GetInstance()->RemoveState(CLevelState::GetInstance());
				CGame::GetInstance()->RemoveState(this);
				return true;
			}
		}
		if (curMove == 2)
		{
			if (m_pDI->KeyPressed(DIK_RETURN))
			{
				SaveGame("game.txt");
				m_bSaved = true;
				return true;
			}
		}
	}


	if (paused == false)
	{
		m_pES->ProcessEvents();
		if(this->m_pPlayer)
		{
			CPlayer* pPlayer = dynamic_cast<CPlayer*>(m_pPlayer);
			if(pPlayer->GetDead()==false)
			{

				if(m_pDI->KeyPressed(DIK_SPACE) && pPlayer->GetPosY() + pPlayer->GetHeight() == CGame::GetInstance()->GetHeight()  || m_pDI->KeyPressed(DIK_SPACE) &&pPlayer->OnPlatform()  || pPlayer->OnEnemy())			
				{
					m_pES->SendEvent("Jumped",this);

					if(pPlayer->GetShield()==false)
					{
						pPlayer->m_cAniInfo.SetName("PlayerJump");
						pPlayer->m_cAniInfo.SetNext("PlayerWalk");
					}
					else
					{
						pPlayer->m_cAniInfo.SetName("PlayerShieldJump");
						pPlayer->m_cAniInfo.SetNext("PlayerShieldWalk");
					}
					pPlayer->m_cAniInfo.SetFrame(0);
					pPlayer->m_cAniInfo.SetTime(0);

					//set jumping animation
					pPlayer->SetVelY(-550);//-350
					pPlayer->OnPlatform(false);
					pPlayer->OnEnemy(false);
				}

				if(m_pDI->KeyDown(DIK_LEFT))
				{	

					if(pPlayer->m_cAniInfo.GetName()!="PlayerWalk"||pPlayer->m_cAniInfo.GetName()!="PlayerJump")
					{
						if(pPlayer->m_cAniInfo.GetCurrentFrame()>=7)
						{
							pPlayer->m_cAniInfo.SetFrame(0);

						}
						if(pPlayer->m_cAniInfo.GetFrameTime()>0.2)
							pPlayer->m_cAniInfo.SetTime(0);
						pPlayer->m_cAniInfo.SetNext("PlayerIdle");
						pPlayer->m_cAniInfo.SetName("PlayerWalk");
					}

					pPlayer->SetVelX(-300);
					pPlayer->OnPlatform(false);
					pPlayer->OnEnemy(false);
					pPlayer->SetFacing(true);
				}
				else if(m_pDI->KeyDown(DIK_RIGHT))
				{
					if(pPlayer->m_cAniInfo.GetName()!="PlayerWalk"||pPlayer->m_cAniInfo.GetName()!="PlayerJump")
					{
						if(pPlayer->m_cAniInfo.GetCurrentFrame()>=7)
						{
							pPlayer->m_cAniInfo.SetFrame(0);

						}
						if(pPlayer->m_cAniInfo.GetFrameTime()>0.2)
							pPlayer->m_cAniInfo.SetTime(0);
						pPlayer->m_cAniInfo.SetNext("PlayerIdle");
						pPlayer->m_cAniInfo.SetName("PlayerWalk");
					}
					pPlayer->SetVelX(300);
					pPlayer->OnPlatform(false);
					pPlayer->OnEnemy(false);
					pPlayer->SetFacing(false);
				}
				else
				{
					pPlayer->SetVelX(0);
				}
				if(m_pDI->KeyPressed(DIK_Q))
				{
					//This is where the first ability code will go
					CSpin* pSpin = dynamic_cast<CSpin*>(this->m_pSpin);
					pSpin->Init();
					pPlayer->AddAbility(pSpin);
					pPlayer->UpdateTimeLeft();
					spinTimer = 3.0f;
					this->m_fMaxAbilityTime = 3.0f;
					pPlayer->SetSpin(true);
				}
				if(m_pDI->KeyPressed(DIK_W))
				{
					CDash* pDash = dynamic_cast<CDash*>(this->m_pDash);
					pDash->Init();
					pPlayer->AddAbility(pDash);
					pPlayer->UpdateTimeLeft();
					this->m_fMaxAbilityTime = 3.0f;
				}
				if(m_pDI->KeyPressed(DIK_E))
				{
					CShield* pShield = dynamic_cast<CShield*>(this->m_pShield);
					pShield->Init();
					pPlayer->AddAbility(pShield);
					pPlayer->UpdateTimeLeft();
					this->m_fMaxAbilityTime = 5.0f;
				}


				if(m_pDI->KeyPressed(DIK_P))								
				{
					m_pSS->Spawn(this->m_pPlayer,"Base",1,200,200,2);
				}
				//COMMENTED OUT BECAUSE, THIS BREAKS THE CODE, PLEASE FIX.----------------------------------------------
				//	m_pSS->Spawn(this->m_pPlayer,"Boss1",1,(this->m_pPlayer->GetPosX()-250),200,0);
				//}
				//COMMENTED OUT BECAUSE, THIS BREAKS THE CODE, PLEASE FIX.----------------------------------------------

				if(m_pDI->KeyPressed(DIK_R))
				{
					if(pPlayer->GetShield()==false)
					{
						pPlayer->m_cAniInfo.SetName("PlayerAttack");
						pPlayer->m_cAniInfo.SetNext("PlayerWalk");
					}
					else
					{
						pPlayer->m_cAniInfo.SetName("PlayerShieldAttack");
						pPlayer->m_cAniInfo.SetNext("PlayerShieldWalk");
					}
					pPlayer->m_cAniInfo.SetFrame(0);
					pPlayer->m_cAniInfo.SetTime(0);

					m_pES->SendEvent("Attacked");
					m_pES->SendEvent("PlayerAttack",this->m_pPlayer);
				}

			}
		}
	}
	return true;
}

void CGameplayState::Update(float fElapsedTime)
{
	m_pMS->ProcessMessages();
	m_pES->ProcessEvents();
	CGame* pGame = CGame::GetInstance();
	if (paused == false)
		this->m_pST->Update(fElapsedTime);
	CPlayer* pPlayer = dynamic_cast<CPlayer*>(this->m_pPlayer);
	if(m_pCam.camPosX>=100&&spawnscount==0)
	{
		m_pSS->Spawn(this->m_pPlayer,"Boss1",1,2000,300,0);
		spawnscount++;	
	}


	if((int)pPlayer->GetPosX() >= m_pCam.camPosX + pGame->GetWidth()/2)
		m_pCam.camPosX = (int)pPlayer->GetPosX() - pGame->GetWidth()/2;
	if(spinTimer > 0.0f)
		spinTimer -= fElapsedTime;
	else
	{
		pPlayer->SetSpin(false);
	}

	m_fPlayerHealthScale = (float)pPlayer->GetHealth() / (float)pPlayer->GetMaxHealth();
	m_fLifeLossScale = (float)pPlayer->GetPotLifeLoss() / (float)pPlayer->GetMaxHealth();
	m_pOM->UpdateEVERYTHING(fElapsedTime);
	m_pOM->CheckCollisions();
	if(pPlayer->GetSpin())
	{
		if(spinTimer < 3.0f && spinTimer > 2.0f)
			pPlayer->SetFacing(!pPlayer->GetFacing());
		if(spinTimer < 2.0f && spinTimer > 1.0f)
			pPlayer->SetFacing(!pPlayer->GetFacing());
		if(spinTimer < 1.0f && spinTimer > 0.0f)
			pPlayer->SetFacing(!pPlayer->GetFacing());
	}
	if (pPlayer->GetHealth() <= 0&&pPlayer->GetDead()==false)
	{
		//float gameTime = GetFrameCount();
		m_pES->SendEvent("Death",this);
		pPlayer->m_cAniInfo.SetFrame(0);
		pPlayer->m_cAniInfo.SetTime(0);
		pPlayer->m_cAniInfo.SetName("PlayerDeath");
		pPlayer->SetDead(true);
	}
	if(pPlayer->GetDead()==true)
	{
		pPlayer->m_cAniInfo.SetName("PlayerDeath");
	}
	if(pPlayer->GetHealth() <= 0&&pPlayer->GetDead()==true&&pPlayer->m_cAniInfo.GetCurrentFrame()==4)
	{
		CHighScoresState::GetInstance()->SetHighScore(CHighScoresState::GetInstance()->GetHighScore());
		//if (frameTimer >= 5)
		//{
		CGame::GetInstance()->RemoveState(this);
		CGame::GetInstance()->RemoveState(CLevelState::GetInstance());
		CGame::GetInstance()->AddState(CGameOverState::GetInstance());
		//}
	}

	int hs = CHighScoresState::GetInstance()->GetHighScore();
	CHighScoresState::GetInstance()->SetHighScore( hs - fElapsedTime );
}

void CGameplayState::Render(void)
{


	//m_pTM->Draw(m_nBackGroundImgID, 0, 0, 1.0f, 1.0f, nullptr, 0, 0, 0); 
	//m_pD3D->GetSprite()->Flush();		//didnt work.


	//	m_pTM->Draw(this->m_nLifeBarImgID, 0, 0, m_fPlayerHealthScale, 1.0f, 0);
	//	m_pTM->Draw(this->m_nLifeBarFrameImgID, 0, 0, 1.0f, 1.0f, 0);
	CPlayer* pPlayer = dynamic_cast<CPlayer*>(m_pPlayer);
	float sPos = (float)pPlayer->GetHealth() - (float)pPlayer->GetPotLifeLoss();

	//	sPos = sPos / 100.0f * 256.0f  ;
	sPos = sPos / 100.0f * 700.0f  ;
	if(sPos<100)
		sPos=100;

	float sPos2 = pPlayer->GetTimeLeft() - m_fMaxAbilityTime;
	//sPos2 = sPos2 / 100.0f * 278.0f;
	sPos2 = sPos2 / 100.0f * 700.0f;
	if(m_fPlayerHealthScale<=-1)
		m_fPlayerHealthScale=0;
	m_pOM->RenderEVERYTHING();
	this->m_pTM->Draw(this->m_nLifeBarImgID, 165,  CGame::GetInstance()->GetHeight()-26, m_fPlayerHealthScale, 1.0f);
	if(pPlayer->GetPotLifeLoss() > 0)
		m_pTM->Draw(this->m_nHealthCostImgID, (int)sPos, CGame::GetInstance()->GetHeight()-26, m_fLifeLossScale, 1.0f, 0);

	this->m_pTM->Draw(this->m_nLifeBarFrameImgID,  100,  CGame::GetInstance()->GetHeight()-80, 1.0f, 1.0f, 0);
	this->m_pTM->Draw(this->m_nSpinButtonImgID,343, CGame::GetInstance()->GetHeight()-70); 


	TCHAR buffer[ 100 ];
	int currentHits = pPlayer->GetCurrentHits();
	_stprintf_s( buffer, 100, _T("Hits: %i"),currentHits);
	m_pD3D->DrawText(buffer, int(pPlayer->GetPosX() + pPlayer->GetWidth()/2 - strlen("Hits: 100")/2*4 - m_pCam.camPosX), int(pPlayer->GetPosY()-40),0,255,255);

	if(pPlayer->GetHitReq() > 0)
	{
		TCHAR buff[100];
		int hitsNeeded = pPlayer->GetHitReq();
		_stprintf_s(buff, 100, _T("/%i"), hitsNeeded);
		m_pD3D->DrawText(buff,int( pPlayer->GetPosX() + pPlayer->GetWidth()/2 - strlen("Hits: 100")/2*4 - m_pCam.camPosX + strlen("Hits: 100")*6), int(pPlayer->GetPosY()-40),0,255,255);
	}

	if(pPlayer->GetTimeLeft() > 0)
	{
		TCHAR timeBuff[100];
		int timeLeft = (int)pPlayer->GetTimeLeft();
		_stprintf_s(timeBuff, 100, _T("Time Left: %i seconds"), timeLeft);
		m_pD3D->DrawText(timeBuff, (int)(CGame::GetInstance()->GetWidth()*.5f)-48, 15,0,255,255);
	}

	TCHAR bu[ 100 ];
	float Times = m_pST->GetTime();
	_stprintf_s( bu, 100, _T("Time: %f"),Times);
	m_pD3D->DrawText(bu, 50,50,255,0,0);


	if (paused)
	{
		m_pFont.Print("PAUSED",100, 100, 2.0f, D3DXCOLOR(255,255,0,255));



		if (curMove == 0)
		{
			m_pFont.Print("Resume", 150, 175, 1.0f, D3DXCOLOR(255,255,255,255));
			m_pFont.Print("Exit",  150, 225, 1.0f, D3DXCOLOR(255,255,0,255));
			m_pFont.Print("Save", 150, 275, 1.0f, D3DXCOLOR(255,255,0,255));
		}
		if (curMove == 1)
		{
			m_pFont.Print("Resume", 150, 175, 1.0f, D3DXCOLOR(255,255,0,255));
			m_pFont.Print("Exit",  150, 225, 1.0f, D3DXCOLOR(255,255,255,255));
			m_pFont.Print("Save", 150, 275, 1.0f, D3DXCOLOR(255,255,0,255));
		}
		if (curMove == 2)
		{
			m_pFont.Print("Resume", 150, 175, 1.0f, D3DXCOLOR(255,255,0,255));
			m_pFont.Print("Exit",  150, 225, 1.0f, D3DXCOLOR(255,255,0,255));
			m_pFont.Print("Save", 150, 275, 1.0f, D3DXCOLOR(255,255,255,255));
			if (m_bSaved)
			{
				m_pFont.Print("Game Saved", 300, 50, 1.0f, D3DXCOLOR(255,255,255,255));
			}
		}
	}
}

void CGameplayState::HandleEvent(CNEvent* pEvent)
{
	if (pEvent->GetEventID()=="Jumped")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nPlayerJumpSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nPlayerJumpSFXID);

		}
		else
		{
			m_pXA->SFXPlaySound(m_nPlayerJumpSFXID);
		}
	}
	if (pEvent->GetEventID()=="Attacked")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nPlayerAttackSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nPlayerAttackSFXID);
		}
		else
		{
			m_pXA->SFXPlaySound(m_nPlayerAttackSFXID);
		}
	}
	if (pEvent->GetEventID()=="Death")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nPlayerDeathSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nPlayerDeathSFXID);
		}
		else
		{
			m_pXA->SFXPlaySound(m_nPlayerDeathSFXID);
		}
	}
	if (pEvent->GetEventID()=="EAttacked")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nEnemyAttackSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nEnemyAttackSFXID);
		}
		else
		{
			m_pXA->SFXPlaySound(m_nEnemyAttackSFXID);
		}
	}					
	if (pEvent->GetEventID()=="EDeath")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nEnemyDeathSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nEnemyDeathSFXID);
		}
		else
		{
			m_pXA->SFXPlaySound(m_nEnemyDeathSFXID);
		}
	}
	if (pEvent->GetEventID()=="BAttacked")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nBossAttackSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nBossAttackSFXID);
		}
		else
		{
			m_pXA->SFXStopSound(m_nBossAttackSFXID);
		}
	}					
	if (pEvent->GetEventID()=="BDeath")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nBossDeadSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nBossDeadSFXID);
		}
		else
		{
			m_pXA->SFXStopSound(m_nBossDeadSFXID);
		}
	}
	if (pEvent->GetEventID()=="BSummon")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nBossSummonSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nBossSummonSFXID);
		}
		else
		{
			m_pXA->SFXStopSound(m_nBossSummonSFXID);

		}

	}					
	if (pEvent->GetEventID()=="PlayerLand")
	{
		if(m_pXA->SFXIsSoundPlaying(m_nPlayerLandSFXID)==false)
		{
			m_pXA->SFXPlaySound(m_nPlayerLandSFXID);
		}
		else
		{
			m_pXA->SFXStopSound(m_nPlayerLandSFXID);

		}
	}


}

void CGameplayState::SaveGame(char * szFilename)
{
	ofstream saveFile(szFilename);

	if (saveFile.is_open())
	{
		if (saveFile.good())
		{
			saveFile << this->m_pCam.camPosX;
			saveFile << "\n";
			saveFile << m_pPlayer->GetPosX();
			saveFile << "\n";
			saveFile << ((CPlayer*)m_pPlayer)->GetHealth();
			//add timer maybe...
		}
	}
}

bool CGameplayState::LoadGame(const char* szFilename)
{	
	paused = false;
	CGame::GetInstance()->AddState(CLevelState::GetInstance());
	//m_pD3D->GetSprite()->Flush();

	m_pOF->RegisterClassType<CPlayer>(L"CPlayer");
	this->m_pPlayer = m_pOF->CreateObject(L"CPlayer");
	((CPlayer*)m_pPlayer)->Init();
	m_pAM->LoadFile("../resources/XML/PLAYERANIMATIONS.xml");	
	m_pAM->SetMap();
	
	//not sure how to add everything correctly...


	ifstream loadFile (szFilename);
	if (loadFile.is_open())
	{
		int camPos;
		int playerPos;
		int playerHealth;
		loadFile >> camPos;
		loadFile >> playerPos;
		loadFile >> playerHealth;

		this->m_pCam.camPosX = camPos;
		m_pPlayer->SetPosX(float(playerPos));
		((CPlayer*)m_pPlayer)->SetHealth(playerHealth);	//need to use playerHealth (instead of GetHealth) ((CPlayer*)m_pPlayer)->GetHealth()
		loadFile.close();
	}
	return true;
}

void CGameplayState::MessageProc(InMessage* pMsg)
{
	CGame* pGame = CGame::GetInstance();
	CGameplayState* self = CGameplayState::GetInstance();
	switch(pMsg->GetMessageID())
	{
	case Msg_DestroyEntity:
		{
			CDestroyEntityMsg* pDestroyMsg = dynamic_cast<CDestroyEntityMsg*>(pMsg);
			CEntity* pEntity = pDestroyMsg->GetEntity();
			self->m_pOM->RemoveObject(pEntity);
		}
		break;
	}
}